var rate = $.cookie("ParcelRate") || 1;
var app_url=location.origin;
(function(){
	var order={
		init:function(){
			//导航栏的选中
            $('.wrapper').navbarscroll();
            $("#categories li").eq(2).addClass("cur").siblings().removeClass("cur");
            this.loadOrder();
			this.BindEvents();
			this.CustomerOrderStatusNum();
		},
		BindEvents:function(){
			var self = this;
			if(APP){
				$("#product-box").remove();
			}
			//订单状态的选中
			$("#OrderStatus span").on("click",function(){
				$(this).addClass("active").siblings().removeClass("active");
				self.loadOrder();
			});
			 //跳转页面
             $("#categories a").on("click",function(){
            	var link_url=$(this).attr("data-href");
//          	if(APP){
//          		var appJson={};
//         			appJson.title=$(this).attr("data-title");
//         			appJson.url=app_url+link_url;	
//         			console.log(JSON.stringify(appJson))
//         			YiYi_APP.showNewPage(JSON.stringify(appJson));
//          	}else{
            		location.href = link_url;
            	//}
            });
			 //取消订单
            $(".order-list").on("click",'span.cancel-order-btn',function () {
            	orderID = $(this).attr("data-id");
            	
			    myApp.prompt(_('请在下面输入框说明取消原因'), _('取消订单'), function (value) {
			    	if(value!="" && $.trim(value).length != 0){
			    		self.cancelOrder(orderID,value);
			    	}else{
			    		 myApp.alert(_('取消原因不能为空！'));
			    	}
			    	//   @*主题色start*@
			        $(document.createElement("script")).attr("src","/Js/ThemesColor.js").appendTo("html");
					//  @*主题色end*@
			    });
          });
            //筛选
            $("#order_term_btn").on("click", function () {
                if(APP){
                	var appJson={};
            		appJson.title=_("订单筛选");
            		appJson.url=app_url+"/Customer/OrderFilter/?type=2";
            		YiYi_APP.showNewPage(JSON.stringify(appJson));
                }else{
                	location.href="/Customer/OrderFilter/?type=2";
                }
            });

            //点击订单到订单详情
            $("#order").on("click",".list_detail_box",function(){
            	var orderId=$(this).attr("data-id");
            	if(APP){
            		var appJson={};
            		appJson.title=_("订单详情");
            		appJson.url=app_url+"/Customer/OrderDetail/?ID="+orderId+"&type=2";
            		YiYi_APP.showNewPage(JSON.stringify(appJson));
            	}else{
            		location.href="/Customer/OrderDetail/?ID="+orderId+"&type=2";
            	}
            });
            //随便逛逛
            $("#go_to_shop").on("click",function(){
            	var link_url=$(this).attr("data-href");
            	if(APP){
            		var appJson={};
					appJson.title=_("统包库存");
           			appJson.function_menu="goods_search";
           			appJson.goods_type="2";
           			YiYi_APP.showExistingPage(JSON.stringify(appJson));
            	}else{
            		location.href=link_url;
            	}
            });
		},
		//统计订单状态的个数
		CustomerOrderStatusNum:function(){
            var requestOrder = {};
            requestOrder.CustomerID = CustomerID; 
            requestOrder.GoodsType=2;
            $.ajax({
                url: "/Team/GetOrderNewNumber",//提交地址
                type: "POST",//提交方式
                data: JSON.stringify(requestOrder),
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    var _result = eval('(' + result + ')');//eval()解析单个json对象
                    var total = 0;
                    if(_result.msgcode== true){
                        _result=_result.msginfo;
                        for (var i = 0; i < _result.length; i++) {
                            if (_result[i].OrderStatus == 1) {
                                $("#status1").text(_result[i].Number);
                                total += _result[i].Number;
                            }
                            if (_result[i].OrderStatus == 2) {
                                $("#status2").text(_result[i].Number);//订单
                                total += _result[i].Number;
                            }
                            if (_result[i].OrderStatus == 3) {
                                $("#status3").text(_result[i].Number);//订单
                                total += _result[i].Number;
                            } 
                            if (_result[i].OrderStatus == 4) {
                                $("#status4").text(_result[i].Number);//订单
                                total += _result[i].Number;
                            } 
                            if (_result[i].OrderStatus == 5) {
                                $("#status5").text(_result[i].Number);//订单
                                total += _result[i].Number;
                            }
                            if (_result[i].OrderStatus == 6) {
                                $("#status6").text(_result[i].Number);//订单
                                total += _result[i].Number;
                            }
                            $("#status0").html(total);//订单总数
                        }
                    }else{
                        $("#status0").text(total);//订单总数
                        $("#status1").text(0);//订单
                        $("#status2").text(0);//订单
                        $("#status3").text(0);//订单
                        $("#status4").text(0);//订单
                        $("#status5").text(0);//订单
                        $("#status6").text(0);//订单
                    }
                }
            });
        },
		//获取参数
		getParameters:function(){
			if(PARAMS!=null){
				var orderParams=PARAMS.orderParams;
			}else{
				var orderParams={};
				orderParams.CustomerID = CustomerID;//客户ID
	            orderParams.PageSize = 100;
	            orderParams.PageIndex = 1;
	            orderParams.OrderStatus=0;
			}
			orderParams.OrderStatus=$("#OrderStatus span.active").attr("data-value");
			return orderParams;
		},
		//加载订单
        loadOrder: function () {
            var self = this;
            myApp.showIndicator();//显示加载中
            var p = self.getParameters();//获取参数
            p.GoodsType=2;
            //获取排序
            var sort = $(".filter-box li i[data-sort-direct]");//获取排序框
            if (p && sort.length > 0 && sort.attr("data-sort-direct")) {//参数和排序长度均大于0,且存在data-sort-direct属性
                p["SortColumn"] = sort.attr("data-sort");//获取排序名称，退点或总价
                p["SortDirect"] = sort.attr("data-sort-direct");//获取排序类型，升序或降序
            }           
            var requestJson = JSON.stringify(p);
            $.ajax({
                url: "/Team/GetOrderNewList",
                type: "POST",
                data: requestJson,
                contentType: "application/json",
                success: function (result) {
                    self.BindingOrder(result);//绑定数据
                    myApp.hideIndicator();//隐藏加载中
                }
            });
        },
        //绑定数据
        BindingOrder: function (result) {
        	
            var _result = eval('(' + result + ')');//eval()解析单个json对象
            if (_result.msgcode == false) {//获取库存失败
                $(".null-cart-tip").show();
                $("#order .order-list").hide();
            } else { //获取库存成功
                $(".null-cart-tip").hide();
                $("#order .order-list").show();
                _result = _result.msginfo;

                var lis = "";//多行数据
                for (var i = 0; i < _result.length ; i++) {//_result.length
					var li = ""; //单行
                    var data = _result[i];
                    var product_data=JSON.parse(data.InventoryAttribute);

                    //统包订单权限start
                    var categoryOrderPower=''; //类型
                        categoryOrderPower="<b>" + GetCategory("parcels",product_data.Category) + "</b>"
                    var colorOrderPower=''; //颜色
                        colorOrderPower="<b>" + GetText("parcels", product_data.Category, product_data.Color, "Color") + "</b>"

                    var weightOrderPower=''; //总重
                        weightOrderPower="<b>"+_("总重") + product_data.Weight + "</b>"

                    var clarityOrderPower=''; //净度
                        clarityOrderPower="<b>" + GetText("parcels", product_data.Category,product_data.Clarity,"Clarity") + "</b>"

                    var placeOrderPower='';// 所在地
                    if(place_td_new==true){
                        placeOrderPower="<b >" + GetText("parcels", product_data.Category, product_data.Place, "Place") + "</b>"
                    }

                    var shapeOrderPower=''; //形状
                        shapeOrderPower= "<b>" + (GetText("parcels", product_data.Category, product_data.Shape, "Shape")) + "</b>"
                    var priceRmbOrderPower=''; //人民币
                    if(RMBPrice_td_new==true){
                        priceRmbOrderPower= "<span class='CurrentRMB'>￥<b class='product_list_price'>" + formatRMBPrice(parseFloat(data.ConfirmRMB*data.InventoryCount)) +"</b></span>"
                    }

                    var priceOrderPower=''; //美元
                    if(price_td_new==true){
                        priceOrderPower="<span class='CurrentUSD'>$<b class='product_list_price'>" + formatUSDPrice(parseFloat(data.ConfirmUSD*data.InventoryCount)) +"</b></span>"
                    }
                    //统包订单权限end

                    lis += "<li><div class='product_list'>"+
                    "<label class='product-parameter' style='display:none'></label>"+
                    "<div class='list_img_box'>"+ShowImgOne(product_data)+"</div>"+
					"<div class='list_detail_box' data-id="+data.OrderID+">"+
					"<p>"+categoryOrderPower+
					"<b>"+_("货号")+product_data.OriginSN+"</b>" +
                    "<b style='float:left'>" +  product_data.Size + "</b>" +
                        colorOrderPower + weightOrderPower+clarityOrderPower+placeOrderPower +shapeOrderPower +
                    "</p>" +
					"<div class='list_price'>"+
                        priceRmbOrderPower+priceOrderPower+
					"<span class='status_box' lang='"+data.OrderStatus+"'>"+CustomerOrderStatusFormat(data.OrderStatus)+"</span></div>"+
					"<div class='time_box'>"+_("下单日期")+"：<span class='order-time'>"+DateTimeFormat(data.CreateTime)+"</span></div>"+
					"</div></div>"+modify_cancel(data)+
					"</li>";

					lis += li;//将单行加到多行
                }
                $("#order .order-list ol").html(lis);//加入到id为inventory的figures中
                currentShow();  //币种设置
                //   @*主题色start*@
			    $(document.createElement("script")).attr("src","/Js/ThemesColor.js").appendTo("html");
			    //  @*主题色end*@
            }
        },
         
        //确定取消订单
        cancelOrder: function (orderID,remarks) {
            var self = this;
            var result = {};
            if(orderID){
            	result.OrderIDs = orderID;
            }else{
            	result.OrderIDs = $("#orderId").html();
            }
            result.GoodsType=2;
            result.Remarks = remarks;
                $.ajax({
                    url: "/Team/CancelOrdersNew",//提交地址
                    type: "POST",//提交方式
                    data: JSON.stringify(result),
                    contentType: "application/json; charset=utf-8",
                    success: function (result) {
                        var _result = eval('(' + result + ')');//eval()解析单个json对象

                        myApp.alert(_(_result["msginfo"]),function(){
                        	self.loadOrder();//加载订单列表
                        });                        
                    }
                });
        },
        
		
	}
	order.init();
})();

function modify_cancel(data){
	var status = data.OrderStatus;
	if(status == 1){
		return "<div class='btn_box'><a href='paymentPage'></a><span class='order_btn cancel-order-btn' data-id='"+data.OrderID+"'>"+_("作废")+"</span></div>";
	}else{
		return "";
	}
}